package com.psm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.JsonRender;
import com.psm.model.SysUser;
import com.psm.pzone.AppUser;
import com.psm.util.MD5;
import com.psm.validator.LoginValid;

/**
 * Created by cheam on 15/6/19.
 */

public class LoginController extends BaseController {
	
	public static List<AppUser> aulist = new ArrayList<AppUser>();
	public static Map auMap = new HashMap<String, AppUser>();

    @Before(LoginValid.class)
    public void login(){
        String user_name=this.getPara("user_name");
        String user_pwd=this.getPara("user_pwd");

        if(user_name==null||"".equals(user_name.trim())||user_pwd==null||"".equals(user_pwd)) {
            this.rendJson_(0, "信息填写不全！");
            return;
        }

        Record user = SysUser.me.qryUser(user_name,user_pwd,"1");

        if(null != user){
            Long maxTime=846000L;
            setCookie("login_user_id",idGenerater.to62(user.getLong("id")),maxTime.intValue());
            rendJson_(1,"登录成功");
        }else{
            rendJson_(0,"帐号或密码不对");
        }
    }
    //app 登录
    public void loginApp(){
    	
    	String user_name=this.getPara("phone");
        String user_pwd=this.getPara("pwd");
        
        Record user = SysUser.me.qryUser(user_name,user_pwd,"1");

        if(null != user){
            setAttr("statu", 0);
            setAttr("user", user);
            Long maxTime=846000L;
            setCookie("login_user_id",idGenerater.to62(user.getLong("id")),maxTime.intValue());
        }else{
        	setAttr("statu", 1);
            setAttr("msg", "用户名或密码错误!");
        }
        render(new JsonRender().forIE());
    }
    //检查手机号码是否可用
    public void checkNum(){
    	String phone = this.getPara("phone");
    	Record p = SysUser.me.checkNum(phone);
    	if(null!= p){
    		setAttr("statu", 1);
    		setAttr("msg", "手机号已注册!");
    	}else{
        	setAttr("statu", 0);
        }
    	render(new JsonRender().forIE());
	}
    
    public void newAppUser(){
    	String uuid=this.getPara("uuid");
    	String phone=this.getPara("phone");
        String pwd=this.getPara("pwd");
        String vcode=this.getPara("vcode");
        
        //TODO:验证码 验证码生成流程需要构建
        if(vcode.equals("888")){
        	AppUser au = new AppUser();
            au.setPhone(phone);
            au.setUuid(uuid);
            au.setPwd(MD5.getMD5ofStr(pwd));
//            aulist.add(au);
            auMap.put(uuid, au);
            setAttr("statu", 0);
        }else{
        	setAttr("statu", 1);
    		setAttr("msg", "验证码错误!");
        }
        render(new JsonRender().forIE());
    }
    public void newAppUserdes(){
    	String type = this.getPara("type");
    	String uuid = this.getPara("uuid");
    	AppUser aUser = (AppUser)auMap.get(uuid);
    	SysUser user = new SysUser();
    	if("temp".equals(type)){
        	aUser.setSex(this.getPara("sex"));
        	aUser.setNickname(this.getPara("nickname"));
        	aUser.setDesc(this.getPara("des"));
        	auMap.put(uuid, aUser);
        	setAttr("statu", 0);
    	}else {
			aUser.setUtype(this.getPara("utype"));
			//TODO:将数据写入数据库
//			int r = SysUser.me.saveUser(aUser);
			user.set("code", aUser.getPhone());
			user.set("name", aUser.getNickname());
			user.set("pwd", MD5.getMD5ofStr(aUser.getPwd()));
			user.set("status", 1);
			user.set("phone", aUser.getPhone());
			user.set("sex", Integer.parseInt(aUser.getSex()));
			user.set("desc", aUser.getDesc());
			user.set("utype", Integer.parseInt(aUser.getUtype()));
			user.set("regtype", 1);
			Boolean b = user.save();
			if(b){
				setAttr("statu", 0);
				setAttr("id", user.getLong("id"));
				Long maxTime=846000L;
	            setCookie("login_user_id",idGenerater.to62(user.getLong("id")),maxTime.intValue());
			}else{
				setAttr("statu", 1);
				setAttr("msg", "注册失败，请重新注册。");
			}
		}
    	
    	render(new JsonRender().forIE());
    }

    public void login_out(){
        removeCookie("login_user_id");
        redirect("/admin/login.html");
    }

}
